<template>
  <div>
    <h3>商品列表</h3>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区 -->
    <el-card class="box-card">
      <!-- 搜索添加区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 给输入框双向数据绑定列表信息，添加可清空点击，刷新列表 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <!-- 给搜索按钮绑定点击事件，搜索input 中的关键字，刷新列表 -->
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表单展示区 -->
      <el-table :data="goodsList" style="width: 100%" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="105px"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80px"></el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="160px">
          <!-- 使用作用域插槽获取时间毫秒，再通过main.js中的全局过滤器调整时间格式 -->
          <template slot-scope="scope">
            {{scope.row.upd_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodsList: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 商品列表展示
    async getGoodsList() {
      // 发送请求，传递关键参数，获取请求数据
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })

      // console.log(res)
      this.total = res.data.total
      this.goodsList = res.data.goods
    },

    // 分页功能
    handleSizeChange(newSize) {
      // console.log(newSize)
      // 将获取的用户选择数值，赋值到data中的queryInfo.pagesize
      this.queryInfo.pagesize = newSize
      // 重新请求参数
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      // console.log(newPage);
      // 将获取的用户选择数值，赋值到data中的queryInfo.pagenum
      this.queryInfo.pagenum = newPage
      // 重新请求参数
      this.getGoodsList()
    },

    // 删除商品
    async deleteGoods(row) {
      // 从element中查询 Message Box 使用方法
      // .than  .catch  分别操作成功和失败，都需要用一个形参来接收对应信息
      const result = await this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((str) => {
          return str
        }).catch((str) => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          }); 
          return str         
        });
      // 如果用户点击确认，则confirmResult 为'confirm'
      // 如果用户点击取消, 则confirmResult获取的就是catch的错误消息'cancel'
      if (result != 'confirm') {
        return
      }
      // 发送请求删除商品，对象解构获取响应数据
      const {data: res} = await this.$http.delete(`goods/${row.goods_id}`)

      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      // 刷新界面
      this.getGoodsList()
    },

    // 添加商品
    addGoods(){
      this.$router.push('/goods/addGoods')
    }
  }
}
</script>

<style lang="less" scoped>
</style>